<template>
  <div class="hm_login">
    <van-nav-bar title="面经登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          square
          block
          type="info"
          color="#FA6D1D"
          native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <router-link class="register" to="/register">注册账号</router-link>
  </div>
</template>

<script>
import { loginApi } from "@/api/user";
import { setToken } from "@/utils/tokens";
export default {
  name: "HmLogin",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  // http://interview-api-t.itheima.net/h5/user/login
  methods: {
    async onSubmit(values) {
      const res = await loginApi(values);
      console.log(res);
      setToken(res.data.token);
      this.$router.push("/article");
    },
  },
};
</script>

<style scoped lang="less">
.hm_login {
  .register {
    color: #069;
    font-size: 12px;
    padding-right: 20px;
    float: right;
  }
}
</style>
